package com.project.gossip.recyclerview;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Locale;

import android.content.Context;
import android.content.res.Configuration;
import android.graphics.Point;
import android.support.v7.widget.RecyclerView;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.project.gossip.Gossip;
import com.project.gossip.R;

public class GossipAdapter extends
		RecyclerView.Adapter<GossipAdapter.ViewHolder> {
	private int rowLayout;
	private Context mContext;
	private List<Gossip> gossips;
	private final int maxLines = 15;

	public GossipAdapter(List<Gossip> gossips, int rowLayout, Context mContext) {
		super();
		this.gossips = gossips;
		this.rowLayout = rowLayout;
		this.mContext = mContext;
	}

	@Override
	public int getItemCount() {
		if (gossips.size() < maxLines) {
			return gossips.size();
		} else {
			return maxLines;
		}
	}

	// setzen des Textes der Listenpunkte
	public void onBindViewHolder(ViewHolder viewholder, int i) {

		Gossip gossip = gossips.get(gossips.size() - 1 - i);

		viewholder.gossipName.setText(gossip.getText());
		WindowManager wm = (WindowManager) mContext
				.getSystemService(Context.WINDOW_SERVICE);
		Display display = wm.getDefaultDisplay();
		Point size = new Point();
		display.getSize(size);
		if (Configuration.ORIENTATION_LANDSCAPE == mContext.getResources()
				.getConfiguration().orientation) {
			viewholder.gossipName.setMaxWidth(size.x * 70 / 100);
		} else {
			viewholder.gossipName.setMaxWidth(size.x * 50 / 100);
		}

		SimpleDateFormat sdf = new SimpleDateFormat("HH:mm'\n'dd.MM.yyyy",
				Locale.GERMANY);
		viewholder.gossipDate.setText(sdf.format(gossip.getDate()));

		switch (gossip.getEmotion()) {
		case BELUSTIGT:
			viewholder.gossipImage.setImageResource(R.drawable.emotion_lime);
			break;
		case GLUECKLICH:
			viewholder.gossipImage.setImageResource(R.drawable.emotion_pink);
			break;
		case ENTAEUSCHT:
			viewholder.gossipImage.setImageResource(R.drawable.emotion_braun);
			break;
		case TRAURIG:
			viewholder.gossipImage.setImageResource(R.drawable.emotion_cyan);
			break;
		case WUETEND:
			viewholder.gossipImage.setImageResource(R.drawable.emotion_rot);
			break;
		default:
			viewholder.gossipImage.setImageResource(R.drawable.ic_plus);
			break;
		}

	}

	@Override
	public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
		View v = LayoutInflater.from(viewGroup.getContext()).inflate(rowLayout,
				viewGroup, false);
		return new ViewHolder(v);
	}

	public static class ViewHolder extends
			android.support.v7.widget.RecyclerView.ViewHolder {
		public TextView gossipName;
		public ImageView gossipImage;
		public TextView gossipDate;

		// public ImageView countryImage;

		public TextView getGossipName() {
			return gossipName;
		}

		public ViewHolder(View itemView) {
			super(itemView);
			gossipName = (TextView) itemView.findViewById(R.id.gossip_text);
			gossipImage = (ImageView) itemView.findViewById(R.id.gossip_image);
			gossipDate = (TextView) itemView.findViewById(R.id.gossip_time);

		}
	}
}
